/**
 * @(#) DailyBizTripProxy.as
 *
 * @date		2009. 04. 6.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 국경일 및 휴일정보 Proxy - Proxy
 * 				월별 국경일 및 휴일 정보를 가져온다.
 */
package com.skt.common_hr.biztrip_nos.model
{
	import com.skt.common_hr.biztrip_nos.BizTripFacade;
	import com.skt.common_hr.biztrip_nos.model.vo.AdjustmentVO;
	import com.skt.common_hr.biztrip_nos.model.vo.BizTripPlaceVO;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class DailyBizTripProxy extends CommonProxy
	{
		public static const NAME:String = "DailyBizTripProxy";
		
		public var places:ArrayCollection;
		public var adjusts:ArrayCollection;
		
		public var outKey:String;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function DailyBizTripProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번,대상월
		 * @return void
		 */
		public function load(key1:String, key2:String):void
		{
			var param:Object = new Object();
			param["I_GWAREKEY01"] = key1;
			param["I_GWAREKEY02"] = key2;
			
			
			loadData("Z_HR_T003", param, resultLoad, faultLoad);
		}
		

		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번,대상월
		 * @return void
		 */
		public function save(type:String, pernr:String, approval:Boolean, status:String, key1:String, key2:String, table1:Array, table2:Array,
				html:String=""):void
		{
			var param:Object = new Object();
			param["I_GWAREKEY01"] = key1;
			param["I_GWAREKEY02"] = key2;
			param["I_PERNR"] 	= pernr;
			//param["I_BUTTON"] 	= type;		// 'A' 승인요청/ 'S' 임시저장
			param["I_REQUEST"]	= approval ? "X" : "";
			param["I_TRSTATUS"]	= status;
			param["I_TRTYPE"] = type;
			
			var tables:Object = new Object();
			tables["ITAB1"] = table1;
			tables["ITAB2"] = table2;
			
			saveData("Z_HR_T006", param, tables, resultSave, faultSave);
		}

		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번,대상월
		 * @return void
		 */
		public function delBizTrip(status:String, key1:String, key2:String):void
		{
			var param:Object = new Object();
			param["I_GWAREKEY01"] = key1;
			param["I_GWAREKEY02"] = key2;
			
			loadData("Z_HR_T004", param, resultDelete, faultDelete);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultLoad(event:ResultEvent):void
		{
			if(retCode==0) {  
				places = parsePlaceList(event.result.OTAB1);
				adjusts = parseAdjust(event.result.OTAB2);
				facade.sendNotification(BizTripFacade.LOAD_DAILY_BIZTRIP_COMPLETE, this);
			}
			else {
				facade.sendNotification(BizTripFacade.LOAD_DAILY_BIZTRIP_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultLoad(event:FaultEvent):void
		{
			facade.sendNotification(BizTripFacade.LOAD_DAILY_BIZTRIP_FAILED, this);
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultSave(event:ResultEvent):void
		{
			if(retCode==0) {  
				outKey = event.result["O_GWAREKEY"];
				facade.sendNotification(BizTripFacade.SAVE_DAILY_BIZTRIP_COMPLETE, this);
			}
			else {
				outKey = "";
				facade.sendNotification(BizTripFacade.SAVE_DAILY_BIZTRIP_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultSave(event:FaultEvent):void
		{
			outKey = "";
			facade.sendNotification(BizTripFacade.SAVE_DAILY_BIZTRIP_FAILED, this);
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultDelete(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(BizTripFacade.DELETE_BIZTRIP_COMPLETE, this);
			}
			else {
				facade.sendNotification(BizTripFacade.DELETE_BIZTRIP_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultDelete(event:FaultEvent):void
		{
			facade.sendNotification(BizTripFacade.DELETE_BIZTRIP_FAILED, this);
		}
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parsePlaceList(srcArr:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in srcArr) 
			{
				var vo:BizTripPlaceVO = new BizTripPlaceVO();
				vo.SEQNO	= rec["SEQNO"];
				vo.BEGDA	= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA	= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.LAND1	= rec["LAND1"];
				vo.LAGBN	= rec["LAGBN"];
				vo.BREN		= rec["BREN"];
				vo.BREN1 	= rec["BREN1"];
				vo.BREN2	= rec["BREN2"];
				list.addItem(vo);
			}
			return list;
		}
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseAdjust(srcArr:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in srcArr) 
			{
				var vo:AdjustmentVO = new AdjustmentVO();
				vo.KOSTL	= rec["KOSTL"];
				vo.WERT1	= rec["WERT1"];
				vo.KDATE	= DateUtil.stringToDateTime(rec["KDATE"]);
				vo.KURSF01	= rec["KURSF01"];
				vo.KURSF02	= rec["KURSF02"];
				vo.DAYPY	= rec["DAYPY"];
				vo.WAERS	= rec["WAERS"];
				if(vo.WAERS == "KRW") {		// 
					vo.WERT1 = Math.round(vo.WERT1*100);
					vo.DAYPY = Math.round(vo.DAYPY*100);
				}
				vo.TRSTATUS	= rec["TRSTATUS"];
				vo.TRSTATX  = rec["TRSTATX"];
				vo.TRTEXT	= rec["TRTEXT"];
				list.addItem(vo);
			}
			return list;
		}
	}
}